package com.cy6;

import java.io.*;

public class CopyFoldersDemo {
    public static void main(String[] args)throws IOException {
        File srcFile = new File("D:\\itcast");
        File destFile= new File("myCharStream\\");
        copy(srcFile,destFile);
    }
    private static void copy (File srcFile, File destFile) throws IOException{
        if(srcFile.isDirectory()){
            String srcFileName = srcFile.getName();
            File newFile = new File(destFile, srcFileName);
            if(!newFile.exists()){
                newFile.mkdir();
            }
            File[] files = srcFile.listFiles();
            for (File file : files) {
                copy(file,newFile);
            }
        }
        else {
            File newFile = new File(destFile, srcFile.getName());
            copy1(srcFile,newFile);
        }

    }
    private static void copy1(File file, File file1) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
        int len;
        byte[] bytes = new byte[1024];
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bis.close();
        bos.close();
    }
}
